package Demo01;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {
    public static void reflectStudent() {
        Class<?> studentClass;
        try {
            studentClass = Class.forName("Demo01.Student");
            Student student = (Student) studentClass.newInstance();
            System.out.println(student);
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectConstructor() {
        Class<?> c = null;
        try {
            c = Class.forName("Demo01.Student");
            //获得构造方法
            Constructor<?> constructor = c.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("zhangsan",11);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e ) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 反射私有属性
     * @param args
     */
    public static void reflectPrivateField() {
        Class<?> c = null;
        try {
            c = Class.forName("Demo01.Student");
            Field field = c.getDeclaredField("name");
            Student student = (Student) c.newInstance();
            field.setAccessible(true);
            field.set(student,"lisi");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }
    public static void reflectPrivateMethod() {
        Class<?> c = null;
        try {
            c = Class.forName("Demo01.Student");
            Method method = c.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            Student student = (Student) c.newInstance();
            method.invoke(student,"yaoxi");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        reflectPrivateMethod();
    }
}
//        reflectPrivateField();
//        reflectStudent();
//        reflectConstructor();
 /*Class<?> c = null;
        try {
            c = Class.forName("Demo01.Student");
            Method method = c.getDeclaredMethod("eat");
            Student student = (Student) c.newInstance();
            method.setAccessible(true);
            method.invoke(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }*/